package cc.deng.wubo.minapp.controller;

import cc.deng.wubo.minapp.beans.game.GmIdiom;
import cc.deng.wubo.minapp.beans.game.GmUser;
import cc.deng.wubo.minapp.beans.game.UserIdiomLog;
import cc.deng.wubo.minapp.core.page.Pagination;
import cc.deng.wubo.minapp.service.FileUploadService;
import cc.deng.wubo.minapp.service.IdiomService;
import cc.deng.wubo.minapp.utils.StringUtil;
import org.nutz.Nutz;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.filepool.NutFilePool;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/app")
public class MinAppController {

    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private NutFilePool tempFilePool;

    @Autowired
    private IdiomService idiomService;
    @Autowired
    Dao dao;


    @RequestMapping("/getIdioms")
    public Object getIdioms(String openid,
                            @RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                            @RequestParam(value = "rows",required = false,defaultValue = "10")Integer rows){
        NutMap results = new NutMap();
        List<GmIdiom> res = new ArrayList<>();


        // 根据openid,获取到游戏用户id
        GmUser gmUser = dao.fetch(GmUser.class, Cnd.where("openId", "=", openid));
        if (gmUser!=null){
            Long uid = gmUser.getId();
            // 判断是否有正在进行的题目,没有返回null 有则返回题目的id
            Long aLong = haveInHand(uid);
            if (aLong!=null){
                // 说明有进行中的题目,将题目加入集合中,并作为第一个元素
                res.add(dao.fetch(GmIdiom.class,aLong));
            }
            // 后面不管是什么样的情况,获取历史题目集合
            List<UserIdiomLog> userIdioms = dao.query(UserIdiomLog.class, Cnd.where("userId", "=", uid));
            if (userIdioms==null||userIdioms.size()<1){
                // 说明该用户一道题目都没有解答过,
                res = new ArrayList<>();
                List<GmIdiom> list = (List<GmIdiom>) idiomService.listPage(page, rows, Cnd.where(null)).getList();
                res.addAll(list);
                return res;

            }else{
                // 获取已经答过的题目集合
                List<Long> idiomIds = userIdioms.stream().map(item -> item.getIdiomId()).collect(Collectors.toList());
                // 通过已经答过的题目,获取新的未打过的题目
                List<GmIdiom> idioms = getNewIdioms(idiomIds,page,rows);

            }



        }else{

        }


        return null;
    }

    private List<GmIdiom> getNewIdioms(List<Long> idiomIds, Integer page, Integer rows) {

        String oldIds = StringUtil.dbIn(idiomIds);
        // 通过排除旧的id集合,获取新的Id
        String sqlstr = "select * from gm_idiom where id not in ("+oldIds+")";
        Sql sql = Sqls.create(sqlstr);
        sql.setPager(new Pager(page,rows));

        System.out.println(1);
        return null;
    }

    /**
     * 查询是否有进行中的题目
     * @return
     */
    public Long haveInHand(Long uid){
        UserIdiomLog fetch = dao.fetch(UserIdiomLog.class, Cnd.where("userId", "=", uid).and("state", "=", 0));
        if (fetch!=null){
            return fetch.getIdiomId();
        }else{
            return null;
        }

    }




}
